//
//  NotiMessageListViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/27.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "NotiMessageListViewController.h"
#import "CommonModel.h"
#import "XiaoJiaNotiListCell.h"
#import "NotiDetailViewController.h"

@interface NotiMessageListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableview;
@property (nonatomic ,strong)NSMutableArray *array;
@property (nonatomic, strong) NSIndexPath *indexPath;
@end

@implementation NotiMessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    self.title = @"通知";
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationHeight, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 170;
    tableview.sectionHeaderHeight = 30;
    tableview.tableFooterView = [[UIView alloc]init];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];
    
    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getSystemNoti];
    }];
    [self.tableview.mj_header beginRefreshing];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getSystemNoti];
    }];
}
-(void)viewDidLayoutSubviews
{
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
#pragma mark - UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *xiaojiacellId = @"XiaoJiaNotiListCell";
        XiaoJiaNotiListCell *cell = [tableView dequeueReusableCellWithIdentifier:xiaojiacellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"XiaoJiaNotiListCell" owner:nil options:0]lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CommonModel *model = self.array[indexPath.row];
        cell.titleLabel.text = model.title?:@"";
        cell.detailLabel.text = model.notice_desc?:@"";
        cell.statusLabel.text = model.is_read ? @"已读":@"未读";
        cell.statusLabel.textColor = model.is_read ? kTextDeepDarkColor:kNavigationColor;
        return cell;
    
}

#pragma mark - UITableViewDelegate
/**设置cell高度*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.indexPath = indexPath;
    CommonModel *mdel = self.array[indexPath.row];
    if (mdel.is_read) {
        NotiDetailViewController *vc = [[NotiDetailViewController alloc]init];
        vc.model = mdel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self updateMessageStatueWith:self.array[indexPath.row]];
    }
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (void)getSystemNoti{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kTokenID forKey:@"token"];
    [params setValue:@"1" forKey:@"page"];
    
    [NetWork PostNetWorkWithUrl:@"api/notice/getAllNotice.ajax" with:params successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        NSArray *temparray = dic[@"data"][@"data"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        [self.array addObjectsFromArray:[CommonModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];

    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];

    }];
}

- (void)updateMessageStatueWith:(CommonModel *)model{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kTokenID forKey:@"token"];
    [params setValue:model.modelId forKey:@"id"];
    
    [NetWork PostNetWorkWithUrl:@"api/notice/update.ajax" with:params successBlock:^(NSDictionary *dic) {
        
        CommonModel *model = self.array[self.indexPath.row];
        model.is_read = YES;
        [self.tableview reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationNone];
        NotiDetailViewController *vc = [[NotiDetailViewController alloc]init];
        vc.model  = model;
        [self.navigationController pushViewController:vc animated:YES];

        
    } FailureBlock:^(NSString *msg) {
        [HUDManager showWarningWithText:msg];

        
    } errorBlock:^(id error) {
       
    }];
}
@end
